package com.example.earthed.model;

import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Data;
import lombok.Getter;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

/**
 * JSON 返回模型
 */
@Data
public class Result<D> implements Serializable {

    //编码：1为成功，0和其他数字表示失败
    private Integer code;

    //错误信息
    private String message;

    //数据
    private D data;

    //动态数据
    private Map map = new HashMap();

    public static <D> Result<D> success(D object) {
        Result<D> r = new Result<>();
        r.data = object;
        r.code = 1;
        return r;
    }

    public static <D> Result<D> error(String message) {
        Result<D> r = new Result<>();
        r.message = message;
        r.code = 0;
        return r;
    }

    public Result<D> add(String key, Object value) {
        this.map.put(key, value);
        return this;
    }
}